package com.systop.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.systop.domain.vo.PayVo;
import lombok.Data;
import org.springframework.stereotype.Component;

@Data
@Component
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000121600482";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCdqdPNZ5j3bIuHs5hOprr6rYvzKtUst3IMDsChHtKXVWj1VDudSWpMIB0ikvtYPUG5MdRZPUo9/psKNkjuK6nuWqLpNyNh2E/CgnRy0PJQ+ILKdMMv09LE9HNZ/3TPBg0ZCLZUyfZLitShfbUGnFpdTu2R4IRrtgQ5eQMHUo88EJP3pYArjSsg2afJUveGaXPTKhD4SwORW1ASpW2vgCC5xxX6YejV8zv5grvUY2AIMShd1ZJka6IOeDakLwx2fCo6F55IcW667nJu4dIxzmWv4o0/1YZ/7bD0p7yoaa+Vce07hOdMEBQPcl3vtPRubYIIIf9QarA+P+w1KLEym/V3AgMBAAECggEAZnCv86g6VZkrV7KzZXcjyjj007yi4fkXmwoC0y1Ju5Z9KdnfGiVXcbrz80D5Gq5Cg+gChzExifJiHlKDNG0eboZpj4qJ7Ms1l7ZXnzpwF3hA0vbm066YJeyG/pTi+ph97iQhBBptc0vzODKLao4wkKv15HCy2aFcQVOFSj60XluJebCp+dBClGwdeYJ8KAeBikjNaXL9gGzjWz7bEPbPYyBNJrRmiqpmZcuNQs2qsY4FJWOxjgdIdtRq1XyAeg7Fu7HPrLLCPxgeLooVaxhk3+mIEx39TxGDS1SA2Y7rHzTW5RVHIQGIObL5HSYQ3GD4r0Bh/QW6wsNU0QJFEbjsgQKBgQDXIHfx7TMmf7CDniSFSE13ls4V7FTAmN0eDGgXbZFdVZaTWE+XXD/veyQugqJ/DIqJQ2MTGscGnKqbX1AfthffRl3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pVh0GDp6PFMmRDyB3WGe0Ys8jixp/S7Z2JbTC8X8sclviS/1DlHovJ565k12nZQcxHtNhZAPoShWtY1zMxfbwtmE2bFJb4g0Wp8t9fpCSh8+wRh+DGzkipZ2MOXTQJMCL";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    // 支付宝公钥
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3sfxFovaSnnzIQykhL6SiUYuzHxXH6DB4ET7ZGrsku2EA3F1dwQ8hrYSBMlEVswm0Rl7bNbIMuabEoRnw/haUS/AjAUX/9+QZzNFloGmP7aveagrm0FxDw1tixSfgOfyVav445nb2LNhDH5Ug1jdJUk92tYCfLrlS+qen3we+olkYiz+pzDJSDtnvWyAnpd1lFG+KwKj6Gpu4ZsUmr3qOGihkMVja0ZgFU7eWoBbY18PVgxM312ffILrAZo6eUB3R2sWJnUHP8GjBVI8d+OtHqUCXPXZ2zhNlvQs1Pw9Tcj7562erkLUbpa8Z7zTUiBexuowE84mZj8vuT+g9JwfmQIDAQAB/haUS/AjAUX/9+QZzNFloGmP7aveagrm0FxDw1tixSfgOfyVav445nb2LNhDH5Ug1jdJUk92tYCfLrlS+qen3we+olkYiz+pzDJSDtnvWyAnpd1lFG+KwKj6Gpu4ZsUmr3qOGihkMVja0ZgFU7eWoBbY18PVgxM312ffILrAZo6eUB3R2sWJnUHP8GjBVI8d+OtHqUCXPXZ2zhNlvQs1Pw9Tcj7562erkLUbpa8Z7zTUiBexuowE84mZj8vuT+g9JwfmQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    // private  String notify_url = "https://539h74664f.goho.co/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";
    private  String notify_url = "https://539h74664f.goho.co/api/CakeShop/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://localhost/#/myOrder";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private  String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
